package com.bookstore.handler;

import com.bookstore.entity.Book;
import com.bookstore.service.BookService;
import com.bookstore.service.UserService;
import com.bookstore.tool.MessageOperate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("book")
public class BookHandler {

    @Resource
    private BookService bookService;
    @Resource
    private UserService userService;
    private MessageOperate messageOperate = new MessageOperate();
    @RequestMapping("getBookByID")
    @ResponseBody
    public Object getBook(Integer bookId, HttpSession Session) {
        Map<String,Object> map = new HashMap<>();
        if(null!= bookId) {
            Book result = bookService.findById(bookId);
            if(null==result){
                map.put("status",false);
                map.put("msg","图书处理:获取图书信息失败！");
            }else{
                map.put("status",true);
                map.put("msg","图书处理:获取图书信息成功！");
                map.put("bookInfo",result);
            }
        } else{
            map = messageOperate.failedRespondDataOption(map,"请求错误:请求数据为空！");
        }
        return map;
    }
    @RequestMapping("getBookByName")
    @ResponseBody
    public Object getBook(String bookName, HttpSession Session) {
        Map<String,Object> map = new HashMap<>();
        if(!(null == bookName)) {
            Book result = bookService.findByName(bookName);
            if(null==result){
                map.put("status",false);
                map.put("msg","图书处理:获取图书信息失败！");
            }else{
                map.put("status",true);
                map.put("msg","图书处理:获取图书信息成功！");
                map.put("bookInfo",result);
            }
        } else{
            map = messageOperate.failedRespondDataOption(map,"请求错误:请求数据为空！");
        }
        return map;
    }
    @RequestMapping("searchBook")
    @ResponseBody
    public Object getBookList(String keyStr, HttpSession Session) {
        Map<String,Object> map = new HashMap<>();
        if(!(null == keyStr)) {
            List<Book> result = bookService.searchBook(keyStr);
            if(result.isEmpty()){
                map.put("status",false);
                map.put("msg","图书处理:搜索失败！");
            }else{
                map.put("status",true);
                map.put("msg","图书处理:搜索成功！");
                map.put("bookList",result);
            }
        } else{
            map = messageOperate.failedRespondDataOption(map,"请求错误:请求数据为空！");
        }
        return map;
    }
}
